﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractFramework;

namespace WpfElevator.ControlPart
{
    class ControllerImpl2 : AController
    {
        public override void addOutsideEvent(AbstractFramework.LogicControl.IEvent inputevent)
        {
            int minDistance = 9999;//最短距离
            int key = 0;//最短距离对应的电梯号
            int floorNum = 10;//楼层数，取不到Config里面的，暂时自己设
            int temp = 0;
            int flag = 0;
            for (int i = 0; i < elevator.Length; i++)
            {

                if (elevator[i].FbdFloor.Contains(inputevent.floor + 1))
                {

                    continue;
                }
                else
                    flag = 1;

                if (elevator[i].State == MovementStatus.Stoped ||//第i个电梯静止
                   (elevator[i].State == MovementStatus.DownMoving && inputevent.updown == 0 && elevator[i].Floor > inputevent.floor) //或者请求方向与电梯运动方向均向下，且电梯楼层在请求楼层之上
                   || (elevator[i].State == MovementStatus.UpMoving && inputevent.updown == 1 && elevator[i].Floor < inputevent.floor))//或者请求方向与电梯运动方向均向上，且电梯楼层在请求楼层之下
                {
                    temp = System.Math.Abs(inputevent.floor - elevator[i].Floor);
                }
                else if ((inputevent.updown == 1 && elevator[i].State == MovementStatus.DownMoving) ||//请求向上运动，电梯向下运动
                       elevator[i].State == MovementStatus.DownMoving && inputevent.updown == 0 && inputevent.floor > elevator[i].Floor)
                {
                    temp = elevator[i].Floor + inputevent.floor;
                }
                else//请求向下运动，电梯向上运动
                {
                    temp = WpfElevator.GlobalConstant.GConstant.MAXELEVATORNUMBER - elevator[i].Floor + WpfElevator.GlobalConstant.GConstant.MAXELEVATORNUMBER - inputevent.floor;
                }
                if (minDistance > temp)
                {
                    minDistance = temp;
                    key = i;
                }
            }
            if (flag == 0)
                return;
            eventIntoList(key, inputevent);
            updateTask(key, taskList[key].floors[0]);
            return;
        }
    }
}
